﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace TestConsole
{
    public class TestHanlder
    {       
        private readonly IHost host;
        public TestHanlder()
        {
            const string baseApiGetway = "http://localhost:5003";
            var builder = new HostBuilder()
                              .ConfigureServices((ctx, svc) =>
                              {
                                  svc.AddHttpClient("IncClient", config =>
                                  {
                                      config.BaseAddress = new Uri($"{baseApiGetway}/SampleTest/IncidentTest");
                                  });

                                  svc.AddHttpClient("SessionClient", config =>
                                  {

                                  });

                                  svc.AddHttpClient();
                                  svc.AddHttpClient<ActionSvcClient>();
                                  svc.AddHttpClient<IncidentSvcClient>();

                              }).UseConsoleLifetime();

            host = builder.Build();
        }

        public TClient Create<TClient>() where TClient : BaseClient
        {
            var client = this.host.Services.GetRequiredService<TClient>();

            return client;
        }

        public HttpClient Create(string name = null)
        {
            var factory = this.host.Services.GetRequiredService<IHttpClientFactory>();

            return factory?.CreateClient(name);
        }
    }
}
